package org.hotcommentssys.controller;

import org.hotcommentssys.entity.Hotevents;
import org.hotcommentssys.service.HoteventsService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Hotevents)表控制层
 *
 * @author makejava
 * @since 2021-12-04 08:43:41
 */
@Controller
@RequestMapping("hotevents")
public class HoteventsController {
    /**
     * 服务对象
     */
    @Resource
    private HoteventsService hoteventsService;

    @RequestMapping(value = "selectAll")
    public String selectAll(Model model) {
        List<Hotevents> hoteventsList = hoteventsService.queryAll(new Hotevents());
        double count = hoteventsList.size();
        double pageCount = count / 3;
        if (pageCount > (int) pageCount) {
            pageCount = (int) pageCount + 1;
        }
        model.addAttribute("pageCount", (int) pageCount);
        model.addAttribute("page", 1);
        List<Hotevents> hotevents = hoteventsService.queryAllByLimit(0, 3, "%%");
        model.addAttribute("hotevents", hotevents);
        return "menu";
    }

    @RequestMapping(value = "selectPage")
    public String selectPage(int page, String keyWord, Model model) {
        Hotevents hotevent = new Hotevents();
        if (keyWord == null) {
            keyWord = "%%";
        } else {
            keyWord = "%" + keyWord + "%";
            hotevent.setKeyword(keyWord);
        }
        System.out.println(keyWord);
        List<Hotevents> hoteventsList = hoteventsService.queryAll(hotevent);
        double count = hoteventsList.size();
        double pageCount = count / 3;
        if (pageCount > (int) pageCount) {
            pageCount = (int) pageCount + 1;
        }
        if (page > pageCount) {
            page = (int) pageCount;
        }
        if (page < pageCount) {
            page = 1;
        }
        List<Hotevents> hotevents = hoteventsService.queryAllByLimit((page - 1)*3, 3, keyWord);
        System.out.println(hotevents);
        model.addAttribute("pageCount", (int) pageCount);
        model.addAttribute("page", page);
        model.addAttribute("keyWord", keyWord);
        model.addAttribute("hotevents", hotevents);
        return "menu";
    }

}
